package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wicket.okrapp.base.service.MOmsTargetCycleService;
import com.wicket.okrapp.base.service.dto.req.*;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.OmsTargetCycleMapper;
import com.wicket.okrapp.dal.po.mbg.OmsTargetCycle;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsTargetCycleServiceImpl
        extends ServiceImpl<OmsTargetCycleMapper, OmsTargetCycle>
        implements MOmsTargetCycleService {

    @Autowired
    OmsTargetCycleMapper omsTargetCycleMapper;

    /**
     * 3-3-07批量改目标周期[2500]
     */
    @Trace(operationName = "3-3-07批量改目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTargetCycle(List<OmsTargetCycle> omsTargetCycle) {

        if (omsTargetCycle == null || omsTargetCycle.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsTargetCycle);

    }

    /**
     * 3-3-07修改目标周期
     */
    @Trace(operationName = "3-3-07修改目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetCycle(OmsTargetCycle omsTargetCycle) {

        return super.updateById(omsTargetCycle);
    }

    /**
     * 3-3-07查目标周期列表[2459]
     */
    @Trace(operationName = "3-3-07查目标周期列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCycle> queryTargetCycleList(QueryTargetCycleListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCycle> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCycle>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-07查目标周期详情
     */
    @Trace(operationName = "3-3-07查目标周期详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetCycle queryTargetCycleDetail(QueryTargetCycleDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            List<OmsTargetCycle> retList = (List<OmsTargetCycle>) super.listByMap(columnMap);
            if (retList == null || retList.size() <= 0) return null;
            OmsTargetCycle ret = retList.get(0);

            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 3-3-07批量增目标周期[2358]
     */
    @Trace(operationName = "3-3-07批量增目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddTargetCycle(List<OmsTargetCycle> omsTargetCycle) {

        if (omsTargetCycle == null || omsTargetCycle.size() == 0) {
            return false;
        }

        return super.saveBatch(omsTargetCycle.stream().map(item -> {
            item.setTargetCycleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setTaskStatus("NOT_AT_THE");
            item.setIsUpdate("TRUE");
            item.setUpdateStatus(("NEW"));
            item.setArchivngType("NOT_ARCHIVE");
            item.setIsEditing("TRUE");
            item.setIsArchive("FALSE");
            item.setSubjectLifeCycle("EDITING");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-3-07新增目标周期[2354]
     */
    @Trace(operationName = "3-3-07新增目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTargetCycle(OmsTargetCycle omsTargetCycle) {
        omsTargetCycle.setTargetCycleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetCycle.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsTargetCycle.setIsUpdate("TRUE");
        omsTargetCycle.setUpdateStatus(("NEW"));
        omsTargetCycle.setArchivngType("NOT_ARCHIVE");
        omsTargetCycle.setIsEditing("TRUE");
        omsTargetCycle.setIsArchive("FALSE");
        omsTargetCycle.setSubjectLifeCycle("EDITING");
        omsTargetCycle.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCycle.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCycle.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCycle.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCycle.setIsValid("FALSE");
        omsTargetCycle.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetCycle.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetCycle)) {
            return omsTargetCycle.getTargetCycleId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-3-07查周期阶段匹配目标周期列表
     */
    @Trace(operationName = "3-3-07查周期阶段匹配目标周期列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCycle> queryCycStaMatchTargetCycleList(QueryCycStaMatchTargetCycleListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCycle> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCycle>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-07批量查内容匹配目标周期
     */
    @Trace(operationName = "3-3-07批量查内容匹配目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCycle> batchQueryContMatchTargetCycle(BatchQueryContMatchTargetCycleReq reqDto) {

        try {
            QueryWrapper<OmsTargetCycle> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getTargetCycleList() == null || reqDto.getTargetCycleList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("target_cycle_content_id", reqDto.getTargetCycleList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCycle>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-07批量查目标周期列表（by目标周期ID）[2194]
     */
    @Trace(operationName = "3-3-07批量查目标周期列表（by目标周期ID）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCycle> batchQueryTargetCycleList(BatchQueryTargetCycleListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCycle> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getExecuteCycleStageList() == null || reqDto.getExecuteCycleStageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("target_cycle_id", reqDto.getExecuteCycleStageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCycle>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-07批量删目标周期
     */
    @Trace(operationName = "3-3-07批量删目标周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTargetCycle(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-3-07批量查目标周期By内容主键
     */
    @Trace(operationName = "3-3-07批量查目标周期By内容主键")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCycle> batchQueryTargetCycleByEntityId(BatchQueryTargetCycleByEntityIdReq reqDto) {

        try {
            QueryWrapper<OmsTargetCycle> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getExecuteCycleStageList() == null || reqDto.getExecuteCycleStageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("target_cycle_content_id", reqDto.getExecuteCycleStageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCycle.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCycle>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
